<?php

/**
 * 模型对应数据库表 "subsite_category".
 *
 * 以下是可用表subsite_category的栏位:
 * @property integer $id
 * @property integer $site_id
 * @property string $pid
 * @property string $title
 * @property string $info
 * @property string $logo
 * @property string $thum
 * @property string $page_title
 * @property string $page_keywords
 * @property string $page_description
 * @property string $product_model
 * @property string $sort
 * @property integer $status
 * @property string $game_id
 */
class SubsiteCategory extends CActiveRecord {

    public $type_array = array(
        '1' => '产品',
        '2' => '资讯',
    );

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return SubsiteCategory the static model class
     * @author nirsh
     * @example
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'web_site_category';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('site_id,title,title_alias', 'required'),
            array('menu,type,site_id, status, infosort_id', 'numerical', 'integerOnly' => true),
            array('pid, sort, game_id', 'length', 'max' => 11),
            array('title, logo, thum, page_title, page_keywords', 'length', 'max' => 255),
            array('product_model', 'length', 'max' => 20),
            array('info, page_description', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_id, pid, title,title_alias, info, logo, thum, page_title, page_keywords, page_description, product_model, sort, status, game_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'site_id' => '网站',
            'pid' => '上级',
            'title' => '名称',
            'info' => '内容',
            'logo' => 'Logo',
            'thum' => '缩略图',
            'page_title' => 'SEO标题',
            'page_keywords' => 'SEO关键字',
            'page_description' => 'SEO描述',
            'product_model' => '模型',
            'sort' => '序号',
            'status' => '状态',
            'game_id' => '游戏',
            'menu' => '菜单显示',
            'type' => '类型',
            'title_alias' => '别名',
            'infosort_id' => '资讯分类'
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('site_id', $this->site_id);
        $criteria->compare('pid', $this->pid);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('title_alias', $this->title_alias, true);
        $criteria->compare('info', $this->info, true);
        $criteria->compare('menu', $this->menu);
        $criteria->compare('type', $this->type);
        $criteria->compare('page_title', $this->page_title, true);
        $criteria->compare('page_keywords', $this->page_keywords, true);
        $criteria->compare('page_description', $this->page_description, true);
        $criteria->compare('product_model', $this->product_model);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('status', $this->status);
        $criteria->compare('game_id', $this->game_id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取分类名称
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getSortNameById($id) {
        $data = $this->findByPk($id);
        return empty($data) ? '一级目录' : $data->title;
    }

    /**
      +-----------------------------------------------------
     * 说明 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getCategoryTitle($cObj) {
        $id = $cObj->id;
        $rStirng = "<span class='title_catagory fl'>┗</span><span class='fl' style='margin-right:20px;'><input size='30' type='text' value='" . $cObj->title . "' field='title' rel='" . $cObj->id . "' onChange='saveChange(this)'></input></span>";
        //存在下级，需要展示下级的按钮
        $vUrl = "#";
        if ($this->IsHaveLowerLevel($id)) {
            $rStirng.= '<a title="展开"  rel="' . $cObj->id . '" href="javaScript:void(0)" onclick="dropLowerLevel(this)" getdata="0" class="gradient-btn" >展开</a>';
        }
        $rStirng.= '<a title="查看" rel="' . $cObj->id . '" parent="1" href="' . Yii::app()->createUrl('subsiteCategory/view', array('id' => $cObj->id)) . '"  class="gradient-btn">查看</a>';
        $rStirng.= '<a title="编辑" rel="' . $cObj->id . '" parent="1" href="' . Yii::app()->createUrl('subsiteCategory/update', array('id' => $cObj->id)) . '"  class="gradient-btn">编辑</a>';
        $rStirng.= '<a title="删除" parent="1"  rel="' . $cObj->id . '"  href="javaScript:void(0)" onclick="deleteCategory(this)" class="gradient-btn">删除</a>';
        return $rStirng;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取目录是否有下级 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function IsHaveLowerLevel($cid) {
        $data = $this->find("pid='" . $cid . "'");
        return empty($data) ? false : true;
    }

    /**
      +-----------------------------------------------------
     * 说明 获取选中菜单 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getCurrectMeun() {
        $category = $_GET['category'];
    }

    /**
      +-----------------------------------------------------
     * 说明获取菜单 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getTopMeun() {
        $current = Yii::app()->getController()->id == 'site' ? 'current' : '';
        $rString = "<ul class='menu'>";
        $rString.= "<li  class='" . $current . "'><a href='" . Yii::app()->BaseUrl . "/'><span>Home</span></a></li>";
        $site_id = Subsite::model()->find("domain='" . Yii::app()->params->domain . "'")->id;
        $cdbc = new CDbCriteria;
        $cdbc->compare('site_id', $site_id);
        $cdbc->compare('menu', 1);
        $cdbc->compare('pid', 0);
        $cdbc->order = "sort ASC";
        $tMenu = $this->findAll($cdbc);
        foreach ($tMenu as $sMenu) {
            $rurl = $this->createCategoryUrl($sMenu);
            $url = is_array($rurl) ? $rurl['url'] : $rurl;
            $current = is_array($rurl) ? $rurl['current'] : '';
            $text = "<span>" . $sMenu->title . "</span>";
            $rString.="<li class='" . $current . "'>";
            $rString.= CHtml::link($text, $url);
            $cData = $this->findAll("pid='" . $sMenu->id . "'");
            if (!empty($cData)) {
                $rString.="<ul class='child'><li class='first-child'></li>";
                foreach ($cData as $scData) {
                    $rurl = $this->createCategoryUrl($scData);
                    $url = is_array($rurl) ? $rurl['url'] : $rurl;
                    $text = $scData->title;
                    $rString.="<li>";
                    $rString.= CHtml::link($text, $url);
                    $rString.="</li>";
                }
                $rString.="<li class='last-child'></li></ul>";
            }
            $rString.="</li>";
        }
        $rString.="</ul>";
        return $rString;
    }

    /**
      +-----------------------------------------------------
     * 说明 生成一级目录
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getTopMeunOnly() {
        $rString = "<ul>";
        $rString.= "<li><a href='" . Yii::app()->BaseUrl . "/'><span>Home</span></a></li>";
        $site_id = Subsite::model()->find("domain='" . Yii::app()->params->domain . "'")->id;
        $cdbc = new CDbCriteria;
        $cdbc->compare('site_id', $site_id);
        $cdbc->compare('menu', 1);
        $cdbc->compare('pid', 0);
        $cdbc->order = "sort ASC";
        $tMenu = $this->findAll($cdbc);
        foreach ($tMenu as $sMenu) {
            $rurl = $this->createCategoryUrl($sMenu);
            $url = is_array($rurl) ? $rurl['url'] : $rurl;
            $text = $sMenu->title;
            $rString.="<li>";
            $rString.= CHtml::link($text, $url);
            $rString.="</li>";
        }
        $rString.="</ul>";
        return $rString;
    }

    /**
      +-----------------------------------------------------
     * 说明 生成目录url
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function createCategoryUrl($cobj) {
        //产品Url
        if ($cobj->type == 1) {
            return $this->getProductSeoUrl($cobj);
        } else {
            return $this->getNewsSeoUrl($cobj);
        }
    }

    /**
      +-----------------------------------------------------
     * 说明 获取导航 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getCategoryBreakTitle($get) {
        $string = isset($get['category']) ? $get['category'] : '';
        $string = isset($get['sub']) ? $string . "/" . $get['sub'] : $string;
        $aString = explode('/', $string);
        $rString = "";
        foreach ($aString as $sString) {
            if ($sString) {
                $cname = $cname ? $cname . "/" . $sString : $sString;
                $name = ucfirst($sString);
                $url = Yii::app()->createUrl($cname);
                $rString.= " » " . CHtml::link($name, $url);
            }
        }
        if ($get['zid']) {
            $rString.= " » " . ProductCategory::model()->findByPk($get['zid'])->title;
        }
        return $rString;
    }

    /**
      +-----------------------------------------------------
     * 说明获取所有导航 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getBreakTitle($get, $type='') {
        $type = $type ? $type : 1;
        $url = Yii::app()->baseUrl;
        $rString = CHtml::link('Home', $url);
        if ($type == 2) {
            $rString.= $this->getNewsBreakTitle($get);
        } else {
            $rString.=$this->getCategoryBreakTitle($get);
        }
        return $rString;
    }

    /**
      +-----------------------------------------------------
     * 说明获取新闻分类标题 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getNewsBreakTitle($get) {
        $string = isset($get['category']) ? $get['category'] : '';
        $aString = explode('/', $string);
        $url = Yii::app()->createUrl('news');
        $rString.= " » " . CHtml::link('News', $url);
        if (!$string) {
            return $rString;
        }
        foreach ($aString as $sString) {
            if ($sString && $get['gid']) {
                $cname = $cname ? $cname . $sString : "news/game" . $sString;
                $name = ucfirst($sString);
                $url = Yii::app()->createUrl($cname . "-" . $get['gid']);
                $rString.= " » " . CHtml::link($name, $url);
            }
        }
        return $rString;
    }

    /**
      +-----------------------------------------------------
     * 说明 生成新闻url
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getNewsSeoUrl($cobj) {
        if ($cobj->pid == 0) {
            if ($cobj->product_model == 'news') {
                $rArray = array(
                    'url' => Yii::app()->createUrl($cobj->product_model),
                    'current' => Yii::app()->getController()->id == 'news' ? 'current' : '',
                );
                return $rArray;
            } else {
                $rArray = array(
                    'url' => Yii::app()->createUrl($cobj->product_model . "/" . $cobj->title_alias),
                    'current' => $_GET['title'] == $cobj->title_alias ? 'current' : '',
                );
                return $rArray;
            }
        } else {
            $cName = $this->getNewSeoCatagory($cobj->id);
            return Yii::app()->createUrl($cobj->product_model . "/" . $cName);
        }
    }

    /**
      +-----------------------------------------------------
     * 说明 访问新闻目录
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getSeoCatagory($pid, &$pStr) {
        $pObj = $this->findByPk($pid);
        if ($pObj->title_alias && $pObj->pid != 0) {
            $rSeo = $rSeo ? $pObj->title_alias . "/" . $rSeo : $pObj->title_alias;
        }
        if ($pObj->pid != 0) {
            $this->getSeoCatagory($pObj->pid, $rSeo);
        }
    }

    /**
      +-----------------------------------------------------
     * 说明 生成产品url
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getProductSeoUrl($cObj) {
        $aliseTitle = $cObj->title_alias;
        if ($cObj->pid) {
            $pObj = $this->findByPk($cObj->pid);
            $pAlise = $pObj->title_alias;
        }
        return array(
            'url' => Yii::app()->createUrl($pAlise . "/" . $aliseTitle),
            'current' => $aliseTitle == $_GET['category'] ? 'current' : ''
        );
    }

    /**
      +-----------------------------------------------------
     * 说明 获取分类
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getSortMeun($type, $gdata=array()) {
        $rstring = "<div class='left-menu'>";
        $cdbc = new CDbCriteria();
        $cdbc->compare('site_id', Yii::app()->params->site_id);
        $cdbc->compare('type', $type);
        if ($type == 2) {
            $cdbc->compare('product_model', 'news');
        }
        $cdbc->compare('pid', 0);
        $cdbc->order = "sort desc";
        $data = $this->findAll($cdbc);
        foreach ($data as $sdata) {
            $active = in_array($sdata->title_alias, $gdata) ? 'active' : '';
            $url = Yii::app()->createAbsoluteUrl($sdata->title_alias);
            //$rstring.= "<div class='left-menu-top'>";
            //$rstring.= CHtml::link($sdata->title, $url, array('class' => 'top ' . $active, 'title' => $scdate->title));
            $cdata = $this->findAll("pid='" . $sdata->id . "'");
            if (!empty($cdata)) {
                foreach ($cdata as $scdate) {
                    $active = in_array($scdate->title_alias, $gdata) ? 'active' : '';
                    $url = Yii::app()->createAbsoluteUrl($sdata->title_alias . "/" . $scdate->title_alias);
                    $rstring.= "<div class='clist " . $active . "'>";
                    $rstring.= CHtml::link($scdate->title, $url, array('class' => $active, 'title' => $scdate->title));
                    $rstring.= "</div>";
                }
            }
            // $rstring.= "</div>";
        }
        $rstring.="</div>";
        return $rstring;
    }

    /**
      +-----------------------------------------------------
     * 说明 通过别名获取产品
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function findCateByAlias($alias) {
        $condition = "site_id='" . Yii::app()->params->site_id . "' And title_alias='" . $alias . "'";
        return $this->find($condition);
    }
    
    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        $size = strlen($val)>50 ? 50 : strlen($val);
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)", 'size' => $size));
    }

}